package com.zhongxiangspace.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.zhongxiangspace.entity.County;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.entity.StationHouse;
import com.zhongxiangspace.entity.VO.QueryAnalyzerOffice;
import com.zhongxiangspace.enums.PagesEnum;
import com.zhongxiangspace.enums.PriceExtenEnum;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.PageResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.AdvertService;
import com.zhongxiangspace.service.ArticleService;
import com.zhongxiangspace.service.CountyService;
import com.zhongxiangspace.service.HouseService;
import com.zhongxiangspace.service.OfficeBuildingService;
import com.zhongxiangspace.service.StationBranchService;
import com.zhongxiangspace.tools.AnalyzerConditionTools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StationListController {
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private AdvertService advertService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StationBranchService stationBranchService;
    @Autowired
    private ArticleService articleService;

    @GetMapping({"lists/h2{value}"})
    public String stationList(@PathVariable("value") String value, Model model) {
        return stationList(value, "pv1", model);
    }

    @GetMapping({"lists/h2{value}/{page}"})
    public String stationList(@PathVariable("value") String value, @PathVariable("page") String page, Model model) {
        long i = System.currentTimeMillis();
        Map<String, Object> map = new HashMap();
        if ((page == "") || (page.length() == 0)) {
            page = "pv1";
        }
        Integer pageSize = PagesEnum.getEnumObject(page).getIndex();
        map.put("url", value);
        String regions = AnalyzerConditionTools.getCondition(value, "r");
        map.put("regions", regions);
        String area = AnalyzerConditionTools.getCondition(value, "a");
        String employees = AnalyzerConditionTools.getCondition(value, "e");
        if (!area.isEmpty()) {
            map.put("area", area);
        } else {
            map.put("area", employees);
        }
        String price = AnalyzerConditionTools.getCondition(value, "p");
        map.put("price", price);
        String decorate = AnalyzerConditionTools.getCondition(value, "d");
        map.put("decorate", decorate);
        QueryAnalyzer tradeAreaQueryAnalyzer = new QueryAnalyzer();
        tradeAreaQueryAnalyzer.setShowCode(Integer.valueOf(1));
        tradeAreaQueryAnalyzer.setHotCode(Integer.valueOf(1));

        List<Map<String, Object>> tradeAreaVOS = this.countyService.queryTradeAreaByKeyword(tradeAreaQueryAnalyzer);
        map.put("tradeArea", tradeAreaVOS);

        String tradeArea_id = AnalyzerConditionTools.getCondition(value, "t");
        map.put("tradeArea_id", tradeArea_id);

        QueryAnalyzer countQueryAnalyzer = new QueryAnalyzer();
        countQueryAnalyzer.setShowCode(Integer.valueOf(1));
        countQueryAnalyzer.setType("0");
        List<County> countyList = this.countyService.queryByKeyword(countQueryAnalyzer);
        map.put("county", countyList);

        QueryAnalyzerOffice queryAnalyzerOffice = new QueryAnalyzerOffice();
        queryAnalyzerOffice.setPage(pageSize);
        County localCounty;
        if (!regions.isEmpty()) {
            queryAnalyzerOffice.setCounty_id(Integer.valueOf(regions.substring(1, regions.length())));
            localCounty = this.countyService.queryOne(Integer.valueOf(regions.substring(1, regions.length())));
        }
        if (!tradeArea_id.isEmpty()) {
            queryAnalyzerOffice.setTradeArea_id(Integer.valueOf(tradeArea_id.substring(1, tradeArea_id.length())));
            localCounty = this.countyService.queryOne(Integer.valueOf(tradeArea_id.substring(1, tradeArea_id.length())));
        }
        if (!price.isEmpty()) {
            queryAnalyzerOffice.setMinPrice(Integer.valueOf(PriceExtenEnum.getEnumObject(price).getMin().intValue() * 10));
            queryAnalyzerOffice.setMaxPrice(Integer.valueOf(PriceExtenEnum.getEnumObject(price).getMax().intValue() * 10));
        }
        String json = new Gson().toJson(queryAnalyzerOffice);
        map.put("queryAnalyzerOffice", json);
        model.addAttribute("data", map);
        return "list/station_list_mobile";
    }

    @ResponseBody
    @PostMapping({"/lists/ajaxStationData"})
    public Result ajxaData(QueryAnalyzerOffice queryAnalyzerOffice) {
        try {
            queryAnalyzerOffice.setType("开放办公");
            PageHelper.startPage(queryAnalyzerOffice.getPage().intValue(), 16, "station_branch.hotCode DESC");
            Page<Map<String, Object>> objectList = (Page) this.stationBranchService.queryList(queryAnalyzerOffice);
            for (Map<String, Object> objectMap : objectList) {
                List<StationHouse> hsmap = this.stationBranchService.queryStationHouse((Integer) objectMap.get("station_id"), "开放办公");
                objectMap.put("stationHouse", hsmap);
            }
            return new PageResult(objectList.getPageNum(), objectList.getPageSize(), objectList.getTotal(), objectList.getPages(), objectList.getResult(), true);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExceptionResult(e);
        }
    }
}
